from django.shortcuts import render,redirect
from .models import userinfo,tablecontent,tablecomment
from hashlib import md5
from django.http import HttpResponse,JsonResponse
import json
from django.core.serializers import serialize
from django.db import transaction
# Create your views here.
def index(request):
    return render(request,'talks/index.html')

def register(request):
    try:
        post=request.POST
        phonename=post.get('account')
        print(post.get('account'))
        print(post.get('password'))
        print(post.get('name'))
        if userinfo.objects.filter(phonename=phonename):
            return HttpResponse(json.dumps({
                "status":1,
                "result":"该账号已经注册过了!"
            }))
        password=md5(post.get('password').encode()).hexdigest()
        username=post.get('name')
        u=userinfo(phonename=phonename,username=username,password=password)
        u.save()
        return  HttpResponse(json.dumps({
            "status":1,
            "result":"注册成功!"
        }))
        
    except:
        return  HttpResponse(json.dumps({
                "status":0,
                "result":"注册失败!"
            }))
def login(request):
    request.session={}
    try:
        phonename=request.POST['account']
        password=md5(request.POST['password'].encode()).hexdigest()
        users=userinfo.objects.filter(phonename=phonename,password=password)
        if users:
            request.session['phonename']=phonename
            request.session['username']=users[0].username
            return HttpResponse(json.dumps({
        "status": 1,
        "result": "登录成功！"
      }))
        else:
            return HttpResponse(json.dumps({
        "status": 0,
        "result": "登录失败！"
      }))
    except:
         return HttpResponse(json.dumps({
        "status": 0,
        "result": "登录失败！"
      }))

def space(request):
    phonename_id=request.GET['account']
    # print(phonename_id)
    return render(request,"talks/space.html",{'account':phonename_id})

def content(request):
    content=[]
    for i in tablecontent.objects.all().values():
        print(i['publishcontent'])
        if(tablecomment.objects.filter(publishcontent=i['publishcontent'])):
            i['comment']=[]
            for e in tablecomment.objects.filter(publishcontent=i['publishcontent']).values():
                i['comment'].append(e)
        content.append(i)
    print(content)
    return JsonResponse(content,safe=False)

def addcontent(request):
    content=request.POST
    publishcontent=content.get('publishcontent')
    publishtime=content.get('publishtime')
    phonename=content.get('phonename_id')
    print(repr(publishcontent))
    print(userinfo.objects.get(phonename=phonename))
    print(repr(phonename))
    add=tablecontent(phonename=userinfo.objects.get(phonename=phonename),publishcontent=publishcontent,publishtime=publishtime)
    add.save()
    return HttpResponse(1)
    
def addcomment(request):
    comment=request.POST
    print(comment)
    commentcontent=comment.get('commentcontent')
    print(commentcontent)
    commenttime=comment.get('commenttime')
    phonename_id=comment.get('phonename_id')
    publishcontent_id=comment.get('publishcontent_id')
    towho=comment.get('towho')
    print(publishcontent_id)
    add=tablecomment(commentcontent=commentcontent,commenttime=commenttime,phonename_id=phonename_id,publishcontent_id=publishcontent_id,towho=towho)
    add.save()
    return HttpResponse(1)